package com.atguigu.srb.core.controller;


import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.service.BorrowInfoService;
import com.atguigu.srb.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * <p>
 * 借款信息表 前端控制器
 * </p>
 *
 * @author fun
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/api/core/borrowInfo")
public class BorrowInfoController {
    @Autowired
    BorrowInfoService borrowInfoService;

    @GetMapping("/getBorrowAmount")
    public R getBorrowAmount(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        BigDecimal borrowAmount = borrowInfoService.getBorrowAmount(userId);
        return R.ok().data("borrowAmount", borrowAmount);
    }

    @GetMapping("/getBorrowInfoStatus")
    public R getBorrowInfoStatus(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (!StringUtils.isEmpty(token)) {
            Long userId = JwtUtils.getUserId(token);
            Integer status = borrowInfoService.getBorrowInfoStatus(userId);
            return R.ok().data("borrowInfoStatus", status);
        }else {
            return R.error();
        }
    }

    @PostMapping("/save")
    public R save(@RequestBody BorrowInfo borrowInfo, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (!StringUtils.isEmpty(token)) {
            Long userId = JwtUtils.getUserId(token);
            borrowInfo.setUserId(userId);
            borrowInfoService.saveBorrowInfo(borrowInfo,userId);
            return R.ok();
        } else {
            return R.error();
        }
    }
}

